package com.study.yangfei.mystudy.activity;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.TextView;

import com.study.yangfei.mystudy.R;
import com.study.yangfei.mystudy.base.BaseTools;
import com.study.yangfei.mystudy.utils.MyService;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;

public class ServiceActivity extends AppCompatActivity {
    final String TAG = BaseTools.Tag(this);
    private MyConnection mConnection = new MyConnection();
    private ActionReciver mActionReciver = new ActionReciver();

    public TextView tvSum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        tvSum =(TextView) findViewById(R.id.tv_sum);

        registerBroadcast();
        startService();

        BaseTools.printClassFunc(this);

        Class<?> demo=null;
        try{
            demo=Class.forName("com.study.yangfei.mystudy.utils.TimingLoggerYF");
        }catch (Exception e) {
            e.printStackTrace();
        }
        //保存所有的接口
        Method intes[]=demo.getMethods();

        for(Method func:intes){
            BaseTools.Logd(this,func.getName());
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unRegisterBroadcast();
        this.unbindService(mConnection);
    }

    private void startService(){
        Intent in = new Intent(this, MyService.class);
        this.bindService(in,mConnection, Context.BIND_AUTO_CREATE);
    }

    private void stopService(){
        this.unbindService(mConnection);
    }

    private void registerBroadcast(){
        IntentFilter ifilter = new IntentFilter();
        ifilter.addAction(MyService.ACTION_SERVICE_STOP);
        ifilter.addAction(MyService.ACTION_SERVICE_UPDATE_SUM);
        this.registerReceiver(mActionReciver,ifilter);
    }

    private void unRegisterBroadcast(){
        this.unregisterReceiver(mActionReciver);
    }

    private void findService(){
        ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> serList =  am.getRunningServices(Integer.MAX_VALUE);
        boolean isfound = false;
        for( ActivityManager.RunningServiceInfo info:serList){
            String sName = info.service.getClassName();
//            Log.d(TAG,sName);
            if(sName.equals("com.study.yangfei.mystudy.utils.MyService")){
                isfound=true;
            }
        }

        if(isfound){
            Log.e(TAG,"MyService is found");
        }else{
            Log.e(TAG,"MyService is not found");
        }
    }

    class MyConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            BaseTools.Logd(this,"onServiceConnected");
            MyService ms = ((MyService.YfBinder)service).getService();
            ms.serviceFun();
//            ms.heaveyWork();
//            findService();
//            stopService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG,"onServiceDisconnected");
            findService();
        }
    }

    class ActionReciver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.d(TAG,"recieve action = "+action);
            if(action.equals(MyService.ACTION_SERVICE_STOP)){
                Log.d(TAG,"recieve ACTION_SERVICE_STOP");
                findService();
            }

            if(action.equals(MyService.ACTION_SERVICE_UPDATE_SUM)){
                double sum = intent.getDoubleExtra(MyService.KEY_SUM,0.f);
                tvSum.setText("sum = "+sum);
            }
        }
    }
}
